package com.hongxun.design.utils.maputil;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.LocationClient;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.geocode.GeoCodeResult;
import com.baidu.mapapi.search.geocode.OnGetGeoCoderResultListener;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;

import com.hongxun.design.R;
import com.hongxun.design.interfaces.UpdateMap;
import com.hongxun.design.listeners.MyLocationListenner;


/**
 * Created by zerdoor_pc on 2016/5/4.
 */
public class BaiduMapDemo extends Activity implements
        OnGetGeoCoderResultListener,UpdateMap {
    BaiduMap mBaiduMap;
    MapView mMapView;
    LatLng currentPt;
    TextView words;
    private String touchType;
    GeoCodeUtil geoCodeUtil = null;
    LocationClient mLocClient; // 定位相关
    public MyLocationListenner myListener ;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mapdemo);
        words = (TextView) findViewById(R.id.words);
        mMapView = (MapView) findViewById(R.id.bmapView);
        mBaiduMap = mMapView.getMap();
        geoCodeUtil = GeoCodeUtil.getInstence(this);
        inItLocation();
        initListener();
    }

    private void inItLocation() {
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);
        // 定位初始化
        myListener = MyLocationListenner.getint(mMapView,this);

    }

    //点击监听
    private void initListener() {

        mBaiduMap.setOnMapTouchListener(new BaiduMap.OnMapTouchListener() {

            @Override
            public void onTouch(MotionEvent event) {

            }
        });

        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            public void onMapClick(LatLng point) {
                touchType = "单击";
                currentPt = point;

                updateMapState();
            }

            public boolean onMapPoiClick(MapPoi poi) {
                return false;
            }
        });
        mBaiduMap.setOnMapLongClickListener(new BaiduMap.OnMapLongClickListener() {
            public void onMapLongClick(LatLng point) {
                touchType = "长按";
                currentPt = point;
                updateMapState();
            }
        });
        mBaiduMap.setOnMapDoubleClickListener(new BaiduMap.OnMapDoubleClickListener() {
            public void onMapDoubleClick(LatLng point) {
                touchType = "双击";
                currentPt = point;
                updateMapState();
            }
        });
        mBaiduMap.setOnMapStatusChangeListener(new BaiduMap.OnMapStatusChangeListener() {
            public void onMapStatusChangeStart(MapStatus status) {
                updateMapState();
            }

            public void onMapStatusChangeFinish(MapStatus status) {
                updateMapState();
            }

            public void onMapStatusChange(MapStatus status) {
                updateMapState();
            }
        });
    }


    /**
     * 更新地图状态显示面板
     */
    public void updateMapState() {
        if (words == null) {
            return;
        }
        String state = "";
        if (currentPt == null) {
            state = "点击、长按、双击地图以获取经纬度和地图状态";
        } else {
            state = String.format(touchType + ",当前经度： %f 当前纬度：%f",
                    currentPt.longitude, currentPt.latitude);
        }
        state += "\n";
        MapStatus ms = mBaiduMap.getMapStatus();
        state += String.format(
                "zoom=%.1f rotate=%d overlook=%d",
                ms.zoom, (int) ms.rotate, (int) ms.overlook);
        if (geoCodeUtil != null && currentPt != null) {
            geoCodeUtil.LatLngToAdress(currentPt);
        }
        words.setText(state);
    }

    @Override
    public void onGetGeoCodeResult(GeoCodeResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(this, "抱歉，未能找到结果", Toast.LENGTH_LONG)
                    .show();
            return;
        }


        setMarka(result.getLocation());
        String strInfo = String.format("纬度：%f 经度：%f",
                result.getLocation().latitude, result.getLocation().longitude);
        Toast.makeText(this, strInfo, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult result) {
        if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) {
            Toast.makeText(this, "抱歉，未能找到结果", Toast.LENGTH_LONG)
                    .show();
            return;
        }
        setMarka(result.getLocation());
        words.append(result.getAddress());
        Toast.makeText(this, result.getAddress(),
                Toast.LENGTH_LONG).show();
    }

    private void setMarka(LatLng point) {
        mBaiduMap.clear();
        mBaiduMap.addOverlay(new MarkerOptions().position(point)
                .icon(BitmapDescriptorFactory
                        .fromResource(R.mipmap.icon_marka)));
//        mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(point));//将位置设为中心点
    }


    @Override
    public void updateMapState(LatLng latLng) {
        if (words == null) {
            return;
        }
        String state = "";
        if (currentPt == null) {
            state = "点击、长按、双击地图以获取经纬度和地图状态";
        } else {
            state = String.format(touchType + ",当前经度： %f 当前纬度：%f",
                    currentPt.longitude, currentPt.latitude);
        }
        state += "\n";
        MapStatus ms = mBaiduMap.getMapStatus();
        state += String.format(
                "zoom=%.1f rotate=%d overlook=%d",
                ms.zoom, (int) ms.rotate, (int) ms.overlook);
        if (geoCodeUtil != null && currentPt != null) {
            geoCodeUtil.LatLngToAdress(currentPt);
        }
        words.setText(state);
    }
}
